﻿namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements.PatternServants;
    using Guia.Locator;

    /// <summary>
    /// This class represents a check-box.
    /// </summary>
    public class CheckBox : UiElement
    {
        /// <summary>
        /// Represents a helper-servant for the Toggle-pattern.
        /// </summary>
        private readonly IToggle toggleServant;

        /// <summary>
        /// Initializes a new instance of the CheckBox class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the CheckBox.</param>
        public CheckBox(AutomationElement automationElement)
            : base(automationElement)
        {
            this.toggleServant = new ToggleImpl(this);
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.CheckBox); }
        }

        /// <summary>
        /// Gets a value indicating whether this CheckBox is checked or not.
        /// </summary>
        public bool IsChecked
        {
            get 
            {
                return this.toggleServant.IsOn; 
            }
        }

        /// <summary>
        /// Check the checkbox.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the CheckBox is disabled.</exception>
        public void Check()
        {
            if (!this.toggleServant.IsOn)
            {
                this.toggleServant.Toggle();
            } 
        }

        /// <summary>
        /// Uncheck the checkbox.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the CheckBox is disabled.</exception>
        public void UnCheck()
        {
            if (!this.toggleServant.IsOff)
            {
                this.toggleServant.Toggle();
            }
        }

        /// <summary>
        /// Toggle the checkbox's value.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the CheckBox is disabled.</exception>
        public void Toggle()
        {
            this.toggleServant.Toggle();
        }
    }
}
